package com.resou.reader.bookdetail.comment.detail;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.BaseComment;
import com.resou.reader.api.entry.CommentBean;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.service.BookService;
import com.resou.reader.api.service.UserService;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.bookdetail.comment.release.IReleaseCommentContract;
import com.resou.reader.mine.IView.UserInstance;
import com.resou.reader.utils.PreferenceUtil;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Author: 萤火虫
 * E-mail:  Allensuns@163.com
 * Description:
 * CreateTime: 2019-04 -11
 **/
public class CommentDetailPresenter extends ResouBasePresenter<ICommentDetailContract.ICommentDetailView> implements ICommentDetailContract.ICommentDetailPresenter, IReleaseCommentContract.IReleaseCommentPresenter {

    BookService bookService;

    public CommentDetailPresenter(ICommentDetailContract.ICommentDetailView view) {
        super(view);
        bookService = ApiImp.getInstance().getService(BookService.class);
    }

    /**
     * 获取评论详情
     *
     * @param id
     * @param page
     */
    @Override
    public void quearCommentDetail(String id, int page) {
        mView.showProgress();
        bookService.reviewCommentView(id, page, pageSize)
                .subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Observer<Result<CommentBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(Result<CommentBean> commentBeanResult) {
                        if (page == 0) {
                            mView.getSmartRefreshLayout().finishRefresh();
                        }
                        if (commentBeanResult.getData().getReviewlist().size() < 15) {
                            mView.getSmartRefreshLayout().finishLoadMoreWithNoMoreData();
                        } else {
                            mView.getSmartRefreshLayout().finishLoadMore();
                        }
                        mView.showCommentDetail(commentBeanResult.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError(e);
                    }

                    @Override
                    public void onComplete() {
                        mView.showContent();
                    }
                });

    }

    /**
     * 发送评论
     *
     * @param content             内容
     * @param novelId             书籍id
     * @param bookReviewCommentId 评论id   如果是评论的回复需要传该字段
     * @param level               星级 1-5
     */
    @Override
    public void sendComment(String content, String novelId, String bookReviewCommentId, int level) {
        mView.showProgress();
        bookService.addComment(UserInstance.getToken(), content, novelId, bookReviewCommentId, level)
                .subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Observer<Result<CommentBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(Result<CommentBean> commentBeanResult) {
                        if (commentBeanResult.getCode() == 0) {
                            mView.sendCommentSuccess();
                        } else {
                            mView.sendCommentFail();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.sendCommentFail();
                    }

                    @Override
                    public void onComplete() {
                        mView.showContent();
                    }
                });
    }

    @Override
    public void reportComment(int type, String beId, String beType) {
        mView.showProgress();
        bookService.report(UserInstance.getToken(), type, beId, beType)
                .subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Observer<Result>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(Result result) {
                        mView.reportSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError(e);
                    }

                    @Override
                    public void onComplete() {
                        mView.showContent();
                    }
                });
    }

    /**
     * 用户点赞接口
     *
     * @param commentId 评论id
     */
    @Override
    public void remoteLikeParise(int commentId) {
        mView.showProgress();
        bookService.likeComment(UserInstance.getToken(), commentId)
                .subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Observer<Result>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(Result result) {
                        if (result.getCode() == 0) {
                            PreferenceUtil.getInstance(mView.getContext()).setlocalCommentPariseState(commentId + "");
                            mView.likePariseSuccess();
                        } else {
                            mView.showError(new Throwable(result.getMsg()));
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError(e);
                    }

                    @Override
                    public void onComplete() {
                        mView.showContent();
                    }
                });

    }


    /**
     * 删除评论
     *
     * @param state     状态 0书评删除 1书评回复删除
     * @param commentId 评论id
     */
    @Override
    public void deleteComment(int state, String commentId) {
        mView.showProgress();
        bookService.deleteComment(UserInstance.getToken(), commentId)
                .subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Observer<Result>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(Result result) {
                        if (result.getCode() == 0) {
                            mView.deleteCommentSuccess(state);
                        } else {
                            mView.showError(new Throwable(result.getMsg() + ""));
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError(e);
                    }

                    @Override
                    public void onComplete() {
                        mView.showContent();
                    }
                });
    }
}
